/**    
 * @{#} PriceSettingActivity.java Create on 2014年11月4日 下午9:41:18    
 *    
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 * @description
 *	
 */
package com.smiier.skin;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import cn.o.app.json.JsonUtil;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.smiier.skin.widget.SwitchButton;
import com.umeng.analytics.MobclickAgent;

public class InvitePatientPriceSettingActivity extends BasicActivity {

	private SwitchButton mSwitchOpenPatientAsk;

	private EditText mEditPrice;

	private LinearLayout mInviteCostLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invite_patient_price_setting);

		mSwitchOpenPatientAsk = (SwitchButton) findViewById(R.id.switch_open_patient_ask);
		mEditPrice = (EditText) findViewById(R.id.edit_price);
		mInviteCostLayout = (LinearLayout) findViewById(R.id.ll_invite_cost);

		init();

		setNavTitle("咨询价格设置");
		setNavRightBtn("保存");

		try {
			JSONArray costList = new JSONArray(
					JsonUtil.convert(GlobalSettings.sUser.CostList));
			for (int i = 0; i < costList.length(); i++) {
				JSONObject object = costList.getJSONObject(i);
				int key = object.getInt("Key");
				if (key == 2) {
					mEditPrice.setText(object.getString("Value"));
				}
			}

			mSwitchOpenPatientAsk
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (isChecked) {
								mInviteCostLayout.setVisibility(View.VISIBLE);
							} else {
								mInviteCostLayout.setVisibility(View.GONE);
							}
						}
					});

			int isAcceptInvite = GlobalSettings.sUser.Is_Accept_Invite;
			if (isAcceptInvite == 1) {
				mSwitchOpenPatientAsk.setChecked(true);
				mInviteCostLayout.setVisibility(View.VISIBLE);
			} else {
				mSwitchOpenPatientAsk.setChecked(false);
				mInviteCostLayout.setVisibility(View.GONE);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		mEditPrice.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence txt, int arg1, int arg2,
					int arg3) {
				if (CommonUtility.isNull(txt)) {
					txt = "0";
				}
				float cost = Float.parseFloat(txt.toString());
				if (cost < 0 || cost > 10000) {
					CommonUtility.tip(activity, "请输入1-10000之间的数字");
					mEditPrice.setText(null);
					return;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
	}

	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.itemview_set_ask_price) {
			Intent intent = new Intent(activity, UpdateInfoInputActivity.class);
			intent.putExtra(Constant.IDENTITY_KEY_PARAM_KEY, "Cost");
			intent.putExtra(Constant.IDENTITY_KEY_1,
					InputType.TYPE_CLASS_NUMBER);
			intent.putExtra(Constant.IDENTITY_KEY_NAV_TITLE, "设置问诊价格");
			intent.putExtra(Constant.IDENTITY_KEY_2, new int[] { 0, 10000 });
			startActivity(intent);
		} else if (id == R.id.text_right) {
			String price = CommonUtility.getText(mEditPrice);
			if (CommonUtility.isNull(price)) {
				price = "0";
			}
			boolean isCheck = mSwitchOpenPatientAsk.isChecked();
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("is_accept_invite", isCheck ? 1 : 0);
			params.put("cost_type", 2);
			try {
				params.put("cost", "1:" + GlobalSettings.sUser.Cost + ",2:"
						+ price);
			} catch (Exception e) {
				e.printStackTrace();
			}
			params.put(Constant.PARAM_API, "User.Set");
			params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
			RequestTaskIntercept.requestIntercept(activity,
					Constant.URL.REQUEST, new RequestHandler(activity,
							new HandlerCallback() {

								@Override
								public void callback(Object o) {
									if (!CommonUtility.isNull(o)) {
										try {
											JSONObject object = (JSONObject) o;
											if (CommonUtility
													.response200(object)) {
												try {
													GlobalSettings.sUser = JsonUtil.convertFromObject(
															object.getJSONObject("Res"),
															User.class);
													finish();
												} catch (Exception e) {
													e.printStackTrace();
												}
											} else {
												CommonUtility.toast(
														activity,
														object.getString(Constant.JSON_PARAM_RESULTMESSAGE));
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							}), params);
		}
	}
	
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
